<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Product extends Vip_Controller
{

    private $uid;

    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->library('form_validation');
        $this->uid = $this->session->userdata('user_id');
        $this->load->model('good_m');
        $this->load->model('category_m');
    }

    public function index($page = 1)
    {

        $limit                      = 10;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['base_url']         = site_url('vip/product/index');
        $config['first_link']       = '首页';
        $config['last_link']        = '尾页';
        $config['prev_link']        = '上一页';
        $config['prev_tag_open']    = '<li class=\'prev pagination__item\'><span class="pagination__number">';
        $config['prev_tag_close']   = '</span></li>';
        $config['cur_tag_open']     = '<li class=\'pagination__item\'><span class="pagination__number pagination__number_active">';
        $config['cur_tag_close']    = '</span></li>';
        $config['num_tag_open']     = '<li class=\' pagination__item\'><span class="pagination__number">';
        $config['num_tag_close']    = '</span></li>';
        $config['next_link']        = '下一页';
        $config['next_tag_open']    = '<li class=\'next pagination__item\'><span class="pagination__number">';
        $config['next_tag_close']   = '</span></li>';
        $config['first_tag_open']   = '<li class=\'first pagination__item\'><span class="pagination__number">';
        $config['first_tag_close']  = '</span></li>';
        $config['last_tag_open']    = '<li class=\'last pagination__item\'><span class="pagination__number">';
        $config['last_tag_close']   = '</span></li>';

        $config['total_rows'] = $this->good_m->count_by_user_id($this->uid);
        $config['per_page']   = $limit;
        $this->pagination->initialize($config);
        $start              = ($page - 1) * $limit;
        $data['pagination'] = $this->pagination->create_links();
        if ($this->auth->is_super_admin()) {
            $goods = $this->good_m->get_all_shop_goods($start, $limit);
        } else {
            $goods = $this->good_m->get_all_shop_goods($start, $limit, $this->uid);
        }
        if (!empty($goods)) {
            foreach ($goods as $key => $val) {
                $good_category                = $this->category_m->get_category_by_id($val['good_category']);
                $goods[$key]['good_category'] = $good_category['category_name'];

                $good_user_info           = $this->user_m->get_user_by_user_id($val['user_id']);
                $goods[$key]['user_name'] = $good_user_info['user_name'];
            }
        }
        $data['goods'] = $goods;
        // 模板下载连接
        $Path                   = 'upload/excel_template/';
        $data['excel_template'] = base_url($Path . "excel_template.xlsx");
        // pp($config);
        // exit();
        $this->load->view('product', $data);
    }

    public function add()
    {

        if ($_POST) {
            $post       = $this->input->post();
            $good_title = isset($post['good_title']) ? htmlspecialchars($post['good_title'], ENT_QUOTES) : false;
            $goods      = [
                'good_title'       => $good_title,
                'good_cover'       => $post['good_cover'],
                'good_category'    => $post['good_category'],
                'good_price'       => $post['good_price'],
                'good_memo'        => $post['good_memo'],
                'good_content'     => $post['good_content'],
                'good_part_number' => $post['good_part_number'],
                'good_add_time'    => time(),
                'good_update_time' => time(),
                'user_id'          => $this->uid,
            ];
            $goodid     = $this->good_m->add_shop_goods($goods);
            if ($goodid) {
                echo json_encode(['status' => 200, "msg" => "新增产品成功"]);
                exit();
            } else {
                echo json_encode(['status' => 400, "msg" => "新增产品失败"]);
                exit();
            }
        }
        // 获取所有分类
        $category          = $this->category_m->get_all_cates();
        $data['category2'] = $category;
        $this->load->view('add_product', $data);
    }

    public function edit($goods_id = '')
    {

        if ($_POST) {
            $post       = $this->input->post();
            $good_title = isset($post['good_title']) ? htmlspecialchars($post['good_title'], ENT_QUOTES) : false;
            $goods      = [
                'good_title'       => $good_title,
                'good_cover'       => $post['good_cover'],
                'good_category'    => $post['good_category'],
                'good_price'       => $post['good_price'],
                'good_memo'        => $post['good_memo'],
                'good_content'     => $post['good_content'],
                'good_part_number' => $post['good_part_number'],
                'good_update_time' => time(),
                'user_id'          => $this->uid,
            ];
            $goodid     = $this->good_m->sava_shop_goods($post['good_id'], $goods);
            if ($goodid) {
                echo json_encode(['status' => 200, "msg" => "新增产品成功"]);
                exit();
            } else {
                echo json_encode(['status' => 400, "msg" => "新增产品失败"]);
                exit();
            }
        }
        // 获取所有分类
        $category          = $this->category_m->get_all_cates();
        $data['category2'] = $category;
        $goods             = $this->good_m->get_shop_goods_by_id($goods_id);
        $data['goods']     = $goods;
        // pp($data);exit();
        $this->load->view('edit_product', $data);
    }

    public function del_pro()
    {
        $post   = $this->input->post();
        $id     = $post['good_id'];
        $result = $this->good_m->delete_shop_goods_by_id($id);
        if ($result) {
            echo json_encode(['status' => 200, "msg" => "删除产品成功"]);
            exit();
        } else {
            echo json_encode(['status' => 400, "msg" => "删除产品失败"]);
            exit();
        }
    }
}